﻿
@{
    ViewBag.Title = "Người dùng chỉ tiêu";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
<div class="page-bar">
    <ul class="page-breadcrumb" style="padding: 4px;">
        <li>
            <i class="fa fa-home"></i>
            @*<a href="#">Quản lý người dùng Quản trị hệ thống các đơn vị</a>*@
            <a href="#">@ViewBag.CurrentMenuName</a>
        </li>
    </ul>
</div>
<div class="row">    
    <div class="col-md-2 col-sm-2" style="">@Html.Label("Năm kế hoạch:")</div>
    <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">
        @Html.DropDownList("NAM_KE_HOACH_ID_SEARCH", ViewBag.DanhSachNamKH as List<SelectListItem>,
                null, new { @class = "form-control input-sm" })
    </div>

    <div class="col-md-2 col-sm-2" style="">@Html.Label("Đơn vị:")</div>
    <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">
        @if (ViewBag.LaDonVi)
        {
            @Html.DropDownList("DON_VI_ID_SEARCH", ViewBag.DanhSachDonVi as List<SelectListItem>,
                                null, new { @class = "form-control input-sm", @disabled = "disabled" })
        }
        else
        {
            @Html.DropDownList("DON_VI_ID_SEARCH", ViewBag.DanhSachDonVi as List<SelectListItem>,
                         null, new { @class = "form-control input-sm", onchange = "onChange_DonVi()" })    
        }
        
    </div>

    <div class="col-md-2 col-sm-2" style="">@Html.Label("Người dùng:")</div>
    <div class="col-md-4 col-sm-4" style="margin-bottom: 0px;">
        @if (ViewBag.LaDonVi)
        {
            @Html.DropDownList("NGUOI_DUNG_ID_SEARCH", ViewBag.DanhSachNguoiDung as List<SelectListItem>, 
                    null, new { @class = "form-control input-sm" })
        }
        else
        {
            @Html.DropDownList("NGUOI_DUNG_ID_SEARCH", ViewBag.DanhSachNguoiDung as List<SelectListItem>, 
                    null, new { @class = "form-control input-sm", @disabled = "disabled" })
        }
    </div>
    
    <div class="col-md-6 col-sm-6">
        <div class="pull-right">
            @*<button id="btnSearch" class="btn btn-sm btn-default"><i class="fa fa-search"></i> Tìm kiếm</button>*@
            <button id="btnAdd" class="btn btn-sm btn-default" style="cursor:pointer;"><i class="fa fa-plus-square-o"></i> Thêm chỉ tiêu </button>
            <div id="divPrintEnum" style="display:none;"></div>
        </div>
    </div>    
</div>
<div id="gridContent" align="center" style="padding:10px;">
    @if (Model.Data != null && Model.Data.Count > 0)
    {
        Html.RenderPartial("_NguoiDungChiTieuList");
    }
    else
    {
        <h4>Không tìm thấy kết quả nào phù hợp</h4>
    }
</div>

<script>
    @functions{
        public string GetAntiForgeryToken()
        {
            string cookieToken, formToken;
            AntiForgery.GetTokens(null, out cookieToken, out formToken);
            return cookieToken + "," + formToken;
        }
    }
    var crrPageIndex = 0;
    var headers = {};
    $(document).ready(function () {
        headers['VerificationToken'] = '@GetAntiForgeryToken()';
        $('#btnAdd').bind('click', function (event) {
            var nguoiDungId = $("#NGUOI_DUNG_ID_SEARCH").val();
            var donViId = $("#DON_VI_ID_SEARCH").val();
            
            if (nguoiDungId == "-1" || donViId == "-1") {
                return;
            }
            functionCreateOrUpdate("Gán chỉ tiêu cho người dùng", "Lưu");           
        });

        $("#btnSearch").on("click", function () {
            OnCriteria(0);
        });       

        $("#NGUOI_DUNG_ID_SEARCH").on("change", function () {
            onChange_User();
        });
        $("#NAM_KE_HOACH_ID_SEARCH").on("change", function () {
            onChange_NamKH();
        });
    });

    function onChange_User() {
        if ($("#NGUOI_DUNG_ID_SEARCH").val() != -1) {
            OnCriteria(0);
        }
    }

    function onChange_NamKH() {
        var userId = $("#NGUOI_DUNG_ID_SEARCH").val();
        if (userId != -1) {
            OnCriteria(0);
        }
    }

    function functionCreateOrUpdate(titleForm, buttonTile) {
        var ajax = $.ajax({
            url: '/NguoiDungChiTieu/CreatePopupUpdate',
            type: 'POST',
            data: {
                NGUOI_DUNG_ID: $("#NGUOI_DUNG_ID_SEARCH").val(),
                TAI_KHOAN: $("#NGUOI_DUNG_ID_SEARCH option:selected").text(),
                DON_VI_ID: $("#DON_VI_ID_SEARCH").val(),
                TEN_DON_VI: $("#DON_VI_ID_SEARCH option:selected").text(),
                NAM_KE_HOACH_ID: $("#NAM_KE_HOACH_ID_SEARCH").val(),
                NAM_KE_HOACH: $("#NAM_KE_HOACH_ID_SEARCH option:selected").val()
            },
            //contentType: "appliction/json",
            success: function (data) {

                var box = bootbox.dialog({
                    title: titleForm,
                    message: data,
                    onEscape: function () {
                        box.modal('hide');
                    },
                    buttons: {
                        success: {
                            label: "<i class='fa fa-plus-square-o'></i> " + buttonTile,
                            className: "btn btn-md btn-success",
                            callback: function () {
                                var form = $('#nguoi-dung-chi-tieu-form');
                                
                                var danhSachMaCap1 = getListChiTieu();
                                if (danhSachMaCap1.length == 0) {
                                    return false;
                                }

                                $.ajax({
                                    url: '/NguoiDungChiTieu/ActionSave',
                                   contentType: 'application/json',
                                    headers: headers,
                                    data: JSON.stringify({ 
                                        model: {
                                            NGUOI_DUNG_ID: $("#NGUOI_DUNG_ID_SEARCH").val(),
                                            TAI_KHOAN: $("#NGUOI_DUNG_ID_SEARCH option:selected").text(),
                                            DON_VI_ID: $("#DON_VI_ID_SEARCH").val(),
                                            TEN_DON_VI: $("#DON_VI_ID_SEARCH option:selected").text(),
                                            NAM_KE_HOACH_ID: $("#NAM_KE_HOACH_ID_SEARCH").val(),
                                            NAM_KE_HOACH: $("#NAM_KE_HOACH_ID_SEARCH option:selected").val()
                                        },
                                        maCap1: danhSachMaCap1
                                    }),
                                    dataType: 'json',
                                    type: 'post',
                                    success: function (response) {
                                        if (response != null && response.status == 200) {
                                            Layout.toastr("", response.result);
                                            OnCriteria(crrPageIndex);
                                            result = true;
                                        } else {
                                            Layout.toastr("", response.result);
                                        }
                                        //$("#divLoading").hide();
                                    },
                                    error: function (errorThrown) {//function(request, textStatus, errorThrown)  {
                                        alert(JSON.stringify(errorThrown));
                                        //alert(request.getAllResponseHeaders());
                                        //alert(request.getResponseHeader('WWW-Authenticate'));
                                    }
                                });

                            }
                        },
                        danger: {
                            label: "<i class='fa fa-reply'></i> Hủy bỏ",
                            className: "btn btn-md btn-default",//"btn-danger",
                            callback: function () {
                            }
                        }
                    }
                });
            },
            complete: function () {
                setTimeout(function () {
                    //$("#HO_VA_TEN").focus();
                }, 500);
            },
            error: function (ex) {
                alert(JSON.stringify(ex));
            }
        });
        return false;
    }

    function getListChiTieu() {
        var values = [];
        $('#checkboxChiTieuChecked input:checked').each(function () {            
            values.push(this.name);
        });
        return values;
    }  

    function OnCriteria(pageIndex) {
        crrPageIndex = pageIndex;
        $.ajax({
            dataType: "html",
            type: "POST",
            url: '/NguoiDungChiTieu/ActionSearch',
            contentType: "application/json",
            data: JSON.stringify({
                pageIndex: pageIndex,
                aModel: {
                    NGUOI_DUNG_ID : $("#NGUOI_DUNG_ID_SEARCH").val(),
                    TAI_KHOAN : $("#NGUOI_DUNG_ID_SEARCH option:selected").text(),
                    DON_VI_ID : $("#DON_VI_ID_SEARCH").val(),
                    TEN_DON_VI : $("#DON_VI_ID_SEARCH option:selected").text(),
                    NAM_KE_HOACH_ID : $("#NAM_KE_HOACH_ID_SEARCH").val(),
                    NAM_KE_HOACH: $("#NAM_KE_HOACH_ID_SEARCH option:selected").val()
                }
            }),
            success: function (response) {
                var $catlist = $('#gridContent');
                if ($catlist == null || $catlist.length == 0) {
                    window.notice('@*@Resources.Localization.MsgItemNotFound*@', window.notice_error);
                }
                else {
                    $catlist.html(response);
                }
            },
            error: function (ex) {
                alert(JSON.stringify(ex));
            }
        });
        //setTimeout(function () { $('#search_form').find('input:text:visible:first').focus(); }, 500);
    }

    function actionDelete() {
        var values = [];
        $('#chkNguoiDungChiTieu input:checked').each(function () {
            values.push(this.name);
        });        
        var box = bootbox.dialog({
            title: "Xóa chỉ tiêu",
            message: "Bạn có chắc chắn xóa (những) chỉ tiêu này không?",
            onEscape: function () {
                box.modal('hide');
            },
            buttons: {
                success: {
                    label: "<i class='fa fa-warning'></i> Xóa",
                    className: "btn btn-sm btn-danger", //btn-success
                    callback: function () {
                        //$("#divLoading").show();
                        $.ajax({
                            url: '/NguoiDungChiTieu/ActionDelete',
                            contentType: 'application/json',
                            headers: headers,
                            data: JSON.stringify({
                                ids: values
                            }),
                            //dataType: 'json',
                            type: 'post',
                            success: function (response) {
                                if (response != null && response.status == 200) {
                                    Layout.toastr("", response.result);
                                    OnCriteria(0);
                                    result = true;
                                } else {
                                    Layout.toastr("", response.result);
                                }
                                //$("#divLoading").hide();
                            },
                            complete: function () {

                            },
                            error: function (ex) {
                                alert(JSON.stringify(ex));
                            }
                        });
                    }
                },
                danger: {
                    label: "<i class='fa fa-reply'></i> Hủy bỏ",
                    className: "btn btn-sm btn-default",//"btn-danger",
                    callback: function () {
                    }
                }
            }
        });
    }

    function onChange_DonVi() {
        var donViId = $("#DON_VI_ID_SEARCH").val();
        $("#NGUOI_DUNG_ID_SEARCH").html('<option value=-1>--- Chọn tài khoản ---</option>');
        $("#NGUOI_DUNG_ID_SEARCH").attr("disabled", "disabled");
        if(donViId > -1){
            $.ajax({
                url: '@Url.Action("GetUserByDonVi")',
                type: 'post',
                dataType: 'json',
                data: {
                    donViId: donViId
                },
                success: function (jsonResult) {
                    //$.each(function (i, item) {});
                    var result = '<option value=-1>--- Chọn tài khoản ---</option>';
                    for (var i = 0, iL = jsonResult.length; i < iL; i++) {
                        result += '<option value="' + jsonResult[i].Value + '">' + jsonResult[i].Text + '</option>';
                    }
                    $("#NGUOI_DUNG_ID_SEARCH").html(result);
                    $("#NGUOI_DUNG_ID_SEARCH").removeAttr("disabled");
                },
                error: function (ex) {
                    alert(JSON.stringify(ex));
                }
            });
        }
    }
  

</script>
